package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.EnterprisesMapper;
import com.danciyixue.boot.dcyx.service.EnterprisesService;
import com.danciyixue.boot.dcyx.model.entity.Enterprises;
import com.danciyixue.boot.dcyx.model.form.EnterprisesForm;
import com.danciyixue.boot.dcyx.model.query.EnterprisesQuery;
import com.danciyixue.boot.dcyx.model.vo.EnterprisesVO;
import com.danciyixue.boot.dcyx.converter.EnterprisesConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 企业信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:40
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class EnterprisesServiceImpl extends ServiceImpl<EnterprisesMapper, Enterprises> implements EnterprisesService {

    private final EnterprisesConverter enterprisesConverter;

    /**
    * 获取企业信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<EnterprisesVO>} 企业信息分页列表
    */
    @Override
    public IPage<EnterprisesVO> getEnterprisesPage(EnterprisesQuery queryParams) {
        Page<EnterprisesVO> pageVO = this.baseMapper.getEnterprisesPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取企业信息表单数据
     *
     * @param id 企业信息ID
     * @return 企业信息表单数据
     */
    @Override
    public EnterprisesForm getEnterprisesFormData(Long id) {
        Enterprises entity = this.getById(id);
        return enterprisesConverter.toForm(entity);
    }
    
    /**
     * 新增企业信息
     *
     * @param formData 企业信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveEnterprises(EnterprisesForm formData) {
        Enterprises entity = enterprisesConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新企业信息
     *
     * @param id   企业信息ID
     * @param formData 企业信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateEnterprises(Long id,EnterprisesForm formData) {
        Enterprises entity = enterprisesConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除企业信息
     *
     * @param ids 企业信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteEnterprisess(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的企业信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
